//
//  ClockWithLayerFilterViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/10.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "ClockWithLayerFilterViewController.h"
#import "UIDevice+XJDevice.h"

@interface ClockWithLayerFilterViewController ()

@property (weak, nonatomic) IBOutlet UIView *hourTensDigit; // 小时十位
@property (weak, nonatomic) IBOutlet UIView *hourSingleDigit; // 小时个位
@property (weak, nonatomic) IBOutlet UIView *minuteTensDigit; // 分钟十位
@property (weak, nonatomic) IBOutlet UIView *minuteSingleDigit; // 分钟个位
@property (weak, nonatomic) IBOutlet UIView *secondTensDigit; // 秒十位
@property (weak, nonatomic) IBOutlet UIView *secondSingleDigit; // 秒个位
@property (nonatomic, strong) NSTimer *time;

@end

@implementation ClockWithLayerFilterViewController

// minificationFilter(图片缩小)和magnificationFilter(图片放大)       minification（缩小图片）和magnification（放大图片）默认的过滤器都是kCAFilterLinear
// kCAFilterLinear 二次线性滤波、kCAFilterTrilinear 三次线性滤波、kCAFilterNearest 最近过滤， 三次线性滤波较二次线性滤波的性能更好。

/**     3种不同的滤波的使用范围
 *  总的来说，对于比较小的图或者是差异特别明显，极少斜线的大图，最近过滤算法会保留这种差异明显的特质以呈现更好的结果。但是对于大多数的图尤其是有很多斜线或是曲线轮廓的图片来说，最近过滤算法会导致更差的结果。换句话说，线性过滤保留了形状，最近过滤则保留了像素的差异
 */

/**
*
补充：
minification（缩小图片）和magnification（放大图片）默认的过滤器都是kCAFilterLinear，这个过滤器采用双线性滤波算法，它在大多数情况下都表现良好。双线性滤波算法通过对多个像素取样最终生成新的值，得到一个平滑的表现不错的拉伸。但是当放大倍数比较大的时候图片就模糊不清了。
 
kCAFilterTrilinear和kCAFilterLinear非常相似，大部分情况下二者都看不出来有什么差别。但是，较双线性滤波算法而言，三线性滤波算法存储了多个大小情况下的图片（也叫多重贴图），并三维取样，同时结合大图和小图的存储进而得到最后的结果。

三线性滤波的好处在于算法能够从一系列已经接近于最终大小的图片中得到想要的结果，也就是说不要对很多像素同步取样。这不仅提高了性能，也避免了小概率因舍入错误引起的取样失灵的问题
就是说三次线性滤波的性能更好。
*/



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIImage *digitImage = [UIImage imageWithName:@"digitNumber"];
    self.hourTensDigit.layer.contents = (__bridge id _Nullable)(digitImage.CGImage);
    self.hourTensDigit.layer.contentsRect = CGRectMake(0, 0, 0.1, 1.0);
    self.hourTensDigit.contentMode = UIViewContentModeScaleAspectFit;
    self.hourTensDigit.layer.magnificationFilter = kCAFilterNearest;
    
    self.hourSingleDigit.layer.contents = (__bridge id _Nullable)(digitImage.CGImage);
    self.hourSingleDigit.layer.contentsRect = CGRectMake(0, 0, 0.1, 1.0);
    self.hourSingleDigit.contentMode = UIViewContentModeScaleAspectFit;
    self.hourSingleDigit.layer.magnificationFilter = kCAFilterNearest;
    
    self.minuteTensDigit.layer.contents = (__bridge id _Nullable)(digitImage.CGImage);
    self.minuteTensDigit.layer.contentsRect = CGRectMake(0, 0, 0.1, 1.0);
    self.minuteTensDigit.contentMode = UIViewContentModeScaleAspectFit;
    self.minuteTensDigit.layer.magnificationFilter = kCAFilterNearest;
    
    self.minuteSingleDigit.layer.contents = (__bridge id _Nullable)(digitImage.CGImage);
    self.minuteSingleDigit.layer.contentsRect = CGRectMake(0, 0, 0.1, 1.0);
    self.minuteSingleDigit.contentMode = UIViewContentModeScaleAspectFit;
    self.minuteSingleDigit.layer.magnificationFilter = kCAFilterNearest;
    
    self.secondTensDigit.layer.contents = (__bridge id _Nullable)(digitImage.CGImage);
    self.secondTensDigit.layer.contentsRect = CGRectMake(0, 0, 0.1, 1.0);
    self.secondTensDigit.contentMode = UIViewContentModeScaleAspectFit;
    self.secondTensDigit.layer.magnificationFilter = kCAFilterNearest;
    
    self.secondSingleDigit.layer.contents = (__bridge id _Nullable)(digitImage.CGImage);
    self.secondSingleDigit.layer.contentsRect = CGRectMake(0, 0, 0.1, 1.0);
    self.secondSingleDigit.contentMode = UIViewContentModeScaleAspectFit;
    self.secondSingleDigit.layer.magnificationFilter = kCAFilterNearest;
    
   self.time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeGo:) userInfo:nil repeats:YES];
    [self.time fire];
    
}

- (void)timeGo:(NSTimer *)timer
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *componts = [calendar components:units fromDate:[NSDate date]];
    NSLog(@"小时：%ld，分钟：%ld，秒：%ld", componts.hour, componts.minute, componts.second);
    // 设置时间
    [self chaneTimeWithDigit:componts.hour / 10 forView:self.hourTensDigit];
    [self chaneTimeWithDigit:componts.hour % 10 forView:self.hourSingleDigit];
    [self chaneTimeWithDigit:componts.minute / 10 forView:self.minuteTensDigit];
    [self chaneTimeWithDigit:componts.minute % 10 forView:self.minuteSingleDigit];
    [self chaneTimeWithDigit:componts.second / 10 forView:self.secondTensDigit];
    [self chaneTimeWithDigit:componts.second % 10 forView:self.secondSingleDigit];
}

// changeTime
- (void)chaneTimeWithDigit:(NSInteger)digit forView:(UIView *)view
{
    view.layer.contentsRect = CGRectMake(digit * 0.1, 0, 0.1, 1);
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

- (IBAction)returnBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
     [self.time invalidate];
    [UIDevice setOrientation:UIInterfaceOrientationPortrait];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
